const data0 = JSON.parse(localStorage.getItem("工资条data"));
const data = data0.data;
// 页面渲染
function renderBody(data, currentPage = 1) {
    singlePageNum = 5;
    renderPaging(data, currentPage);
    const statIndex = singlePageNum * (currentPage - 1);
    const endIndex = statIndex + singlePageNum;
    const newData = data.slice(statIndex, endIndex);
    $("#information tbody").empty();
    for (let i = 0; i < newData.length; ++i) {
        const checked = data[i].isComplete ? "checked" : "";
        $("tbody").append(`<tr> <td><input type="checkbox" ${checked}> ${newData[i].name}</td>
        <td>${newData[i].id}</td>
        <td>${newData[i].phone}</td>
        <td>${newData[i].department}</td>
        <td>${newData[i].position}</td>
        <td>${newData[i].type}</td>
        <td>${newData[i].state}</td>
            <td>
            <button type="button" class="btn btn-info particulars" id="${newData[i].id}">确认发送</button>
            </td>
        </tr>
            `
        );
    }
}

function renderPaging(data, currentPage = 1) {
    $("#pages").empty();
    const num = Math.ceil(data.length / singlePageNum);
    for (let i = 1; i <= num; ++i) {
        $("#pages").append(`<span>${i}</span>`);
    }
    $("#pages > span").eq(currentPage - 1).addClass("currentPage");
}

renderBody(data);

// 页码
$("#pages").on("click", "span", function (event) {
    const currentPage = parseInt($(event.target).text());
    renderBody(data, currentPage);
});
// 上一页
$("#pre").on("click", function () {
    const currentPage = parseInt($("#pages > .currentPage").text());
    if (currentPage === 1) {
        return;
    }
    else {
        renderBody(data, currentPage - 1);
    }
});
// 下一页
$("#next").on("click", function () {
    const currentPage = parseInt($("#pages > .currentPage").text());
    if (currentPage === $("#pages span").length) {
        return;
    }
    else {
        renderBody(data, currentPage + 1);
    }
});

let a = 1;
// 全选
$("#all").on("click", function () {

    if (a % 2 === 1) {
        if ($("#information td input").prop("checked") === false) {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = true;
            }
        }
        else {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = true;
            }
        }
    }
    else {
        if ($("#information td input").prop("checked") === false) {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = false;
            }
        }
        else {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = false;
            }
        }
    }
    a = a + 1;
    renderBody(data);
});

// 搜索
$("#reset").on("click", function () {
    $("#screen").empty();
    let matter = $(".condition input").val();
    $("#screen").append(`
    <span class="screen">筛选条件：</span>
    <span class="trem ">${matter}</span>
    `);
    let newData = [];
    for (let i = 0; i < data.length; ++i) {
        for (let key in data[i]) {
            if (matter === data[i][key]) {
                newData.push(data[i]);
            }
        }
    }
    renderBody(newData);
});

$("tbody").on("click", ".particulars", function (event) {
    dataId1 = parseInt($(event.target).attr("id"));
    console.log(dataId1)
    for (let i = 0; i < data.length; ++i) {
        if (dataId1 === data[i].id) {
            data[i].state = "已发送";
            data[i].isComplete = true;
        }
    }   
    renderBody(data);
});


$("#newadd").on("click",function(){
    window.location.href ="../薪资/工资条.html"
})